int currentSent, lastSent=0, currentRedundant=0,lastRedundant=0, currentReceived=0, lastReceived=0;
int lastDelivery=0, currentDelivery=0, currentLost=0, lastLost=0;
int lastNR=0,currentNR=0;
double lastavg=0,currentavg=0;
double latency_vector[2000];
//------------------------------------------------------------------------------
void store_performance_in_file()
{
	FILE *o_fp;
	int i;
	char name[STRING_LENGTH], ID[STRING_LENGTH];
	//------------------------------------------
	o_fp = fopen(result_file,"a");
	assert (o_fp != NULL);
	for(i = 0; i < throughput_counter; i++)
	  	fprintf(o_fp,"%d %d %d %f %d %d %f %d %d %d %d %d %f\n",results[i].total_time,results[i].HD,results[i].delivered,results[i].avg_throughput,results[i].interval_time, results[i].interval_delivery,results[i].interval_throughput, results[i].totalHole,results[i].intervalHole, results[i].intervalRedundant,results[i].totalRedundant, results[i].lost, results[i].avg_m_latency);
	fclose(o_fp);
	//-----------------Announce finishing---------------------
	strcpy(name,"/home/parisa/FINISH/l");
	sprintf(ID,"%d", PROCESS_ID);
	strcat(name,ID);
	strcat(name,".txt");
	o_fp = fopen(name,"a");
	assert (o_fp != NULL);
	fprintf(o_fp,"%d\n", throughput_counter);
	fclose(o_fp);
	//-----------------Announce finishing---------------------	
}
//------------------------------------------------------------------------------
void avg_throughput()
{
	int i;
	double avg_t=0, avg_l=0;
	
	for(i = 0; i <= throughput_counter; i++)
	  {
		  avg_t += throughput_vector[i];
		  avg_l += latency_vector[i];
	  }
	
	results[throughput_counter].avg_throughput = avg_t/(double)(throughput_counter+1);
	results[throughput_counter].avg_m_latency  = avg_l/(double)(throughput_counter+1);
}

//------------------------------------------------------------------------------
void compute_performance(int sd, short event, void* arg)
{
	int msecInterval, msecTotal, dr=0, lostInterval=0;
	double t;
	
	if(r_tag)
	  {
		  currentDelivery	=	deliveryCount;
		  currentavg		=	avg_latency;
		  currentRedundant	=	redundant;
		  currentReceived	=	RECEIVED;
		  currentLost		=	LOST;
		  currentNR			=	nr;
		  
		  gettimeofday(&performance_end_time,NULL);
		  gettimeofday(&endTime,NULL);
		  //============================Calculate Time====================================
		  throughput_elapsed_time.tv_sec	=	performance_end_time.tv_sec-performance_start_time.tv_sec;
		  throughput_elapsed_time.tv_usec	=	(performance_end_time.tv_usec>performance_start_time.tv_usec? performance_end_time.tv_usec-performance_start_time.tv_usec: performance_start_time.tv_usec-performance_end_time.tv_usec);
		  msecInterval	=	1000*throughput_elapsed_time.tv_sec+throughput_elapsed_time.tv_usec/1000;
		  
		  elapsedTime.tv_sec	=	endTime.tv_sec-startTime.tv_sec;
		  elapsedTime.tv_usec	=	(endTime.tv_usec>startTime.tv_usec? endTime.tv_usec-startTime.tv_usec: startTime.tv_usec-endTime.tv_usec);
		  msecTotal	=	1000*elapsedTime.tv_sec+elapsedTime.tv_usec/1000;
		  
		  lostInterval	=	currentLost-lastLost;
		  dr			=	currentDelivery-lastDelivery;
		  
		  if(f)
			{
				losts[lost_counter].msecInterval	=	msecInterval;
				losts[lost_counter].msecTotal		=	msecTotal;
				lost_counter++;
				losts[lost_counter].count=0;
				f	=	false;
			}
		  
		  t=0;
		  double ii	=	(double)sizeof(inst);
		  if(msecInterval!=0){
			throughput_vector[throughput_counter]	=	 (1000*8*((double)dr*ii))/(double)(msecInterval);
			throughput_vector[throughput_counter]	=	 throughput_vector[throughput_counter] /(double)(1048576);
			assert(throughput_vector[throughput_counter] >= 0);
		  }
		  else
			  throughput_vector[throughput_counter]=0;
		  
		  //============================Announce the Result===============================
		  latency_vector[throughput_counter]=((currentavg-lastavg)/(double)(dr*MAX_MSG_COUNT));
		  avg_throughput(currentNR-lastNR, nr);
		  
		  results[throughput_counter].interval_throughput	=	throughput_vector[throughput_counter];
		  results[throughput_counter].intervalRedundant		=	currentRedundant-lastRedundant;
		  results[throughput_counter].avg_m_latency			=	latency_vector[throughput_counter];
		  results[throughput_counter].interval_time			=	msecInterval;
		  results[throughput_counter].intervalHole			=	currentNR-lastNR;
		  results[throughput_counter].delivered				=	deliveryCount;
		  results[throughput_counter].totalRedundant		=	redundant;
		  results[throughput_counter].HD					=	highestDeliverable;
		  results[throughput_counter].total_time			=	msecTotal;
		  results[throughput_counter].interval_delivery		=	dr;
		  results[throughput_counter].lost					=	lostInterval;
		  results[throughput_counter].totalHole				=	nr;
	
		  throughput_counter++;
		  
		  lastRedundant	=	currentRedundant;
		  lastDelivery	=	currentDelivery;
		  lastLost		=	currentLost;
		  lastavg		=	currentavg;
		  lastNR		=	currentNR;
		  
		  int i	=	throughput_counter	-	1;
		  printf("%d %d %d %f %d %d %f %d %d %d %d %f %d %d\n",results[i].total_time,results[i].HD,results[i].delivered,results[i].avg_throughput,results[i].interval_time, results[i].interval_delivery,results[i].interval_throughput, results[i].totalHole,results[i].intervalHole, results[i].intervalRedundant,results[i].totalRedundant, results[i].avg_m_latency, results[i].lost, deliveryCount);
		  
		if(msecTotal >= 120000){
			fprintf("WRITING TO FILE\n");
			store_performance_in_file();
			sleep(100);
			exit(1);
		}
		gettimeofday(&performance_start_time,NULL);
	  }
  evtimer_add(timeout_ev,&tv);
}
